package org.ghost.quickstart.scalac.array

import java.text.MessageFormat

import org.ghost.quickstart.scalac.traits.BaseTrait

import scala.util.Random

object ArrayLoop extends BaseTrait {
  override def print(): Unit = {
    val aArray = new Array[String](4)
    aArray(0) = "哈哈"
    aArray(1) = "Hello"
    aArray(2) = "World"
    aArray(3) = "测试"
    val bArray = Array(1, 17, 51, 57, 98, 21, 3, 8, 544, 69, 47, 55)

    //循环遍历
    println("*****循环遍历数组")
    for (a <- aArray) {
      println("aArray ->" + a)
    }

    println("*****循环遍历数组求和")
    var sum = 0
    for (a <- bArray) {
      sum += a
    }
    println("bArray ->sum=" + sum)

    println("*****循环遍历数组求最小值")
    var min = bArray(0)
    var max = bArray(0)
    for (i <- 1 until bArray.length) {
      if (bArray(i) < min) {
        min = bArray(i)
      }
      if (bArray(i) > max) {
        max = bArray(i)
      }
    }
    println("bArray ->min=" + min + ",max=" + max)

    //多维数组
    val myMatrix = Array.ofDim[Int](3, 3)
    println("*****多维数组操作")
    for (a <- 0 until 3; b <- 0 until 3) {
      myMatrix(a)(b) = Random.nextInt()
    }
    for (a <- 0 until 3; b <- 0 until 3) {
      println(MessageFormat.format("myMatrix[{0}][{1}]={2}", String.valueOf(a), String.valueOf(b), String.valueOf(myMatrix(a)(b))))
    }

    //合并数组
    println("*****合并数组操作Array.concat:")
    for (a <- Array.concat(Array(111, 222, 333, 444, 555, 666), Array.range(100, 120))) {
      println(a)
    }

  }
}
